﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using LowLevelGraphics;
using LowLevelGraphics.Filter;

namespace ImageRecognition2.Forms
{
    public partial class FormPixelScanner : Form
    {
        protected EXPERIMENTAL1 m_EXPERIMENTAL1 = EXPERIMENTAL1.TOARGB;
        protected Bitmap m_Bitmap = null;
        protected bool m_bPadding = false;

        /// <summary>
        /// Default constructor
        /// </summary>
        public FormPixelScanner()
        {
            InitializeComponent();

            this.chkbPadding.Checked = m_bPadding;
        }

        /// <summary>
        /// primary constructor
        /// </summary>
        /// <param name="_bitmap"></param>
        public FormPixelScanner(Bitmap _bitmap)
        {
            InitializeComponent();

            pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
            m_Bitmap = _bitmap;
            nudX.Minimum = 0;
            nudX.Maximum = _bitmap.Width - 1;

            nudY.Minimum = 0;
            nudY.Maximum = _bitmap.Height - 1;

            nudWidth.Minimum = 1;
            nudWidth.Maximum = _bitmap.Width - 1;

            nudHeight.Minimum = 1;
            nudHeight.Maximum = _bitmap.Height - 1;

            foreach(KeyValuePair<EXPERIMENTAL1, ColorCalculation> keyValuePair in Definitions.ColorCalculationDictionary)
            {
                this.cobMode.Items.Add(keyValuePair.Key);
            }
            this.cobMode.SelectedIndex = 0;
        }

        /// <summary>
        /// Displays the values
        /// </summary>
        public void DisplayValues()
        {
            this.dataGridView1.SuspendLayout();
            Bitmap bitmapDisplay = (Bitmap)m_Bitmap.Clone();
            Graphics g = Graphics.FromImage(bitmapDisplay);
            g.DrawRectangle(Pens.Blue, new Rectangle((int)nudX.Value, (int)nudY.Value, (int)nudWidth.Value, (int)nudHeight.Value));
            pictureBox1.BackgroundImage = bitmapDisplay;
            pictureBox1.Invalidate();

            UnsafeBitmap bitmap = new UnsafeBitmap(m_Bitmap);

            int nMinX = (int)nudX.Value;
            int nWidth = (int)nudWidth.Value;
            int nMinY = (int)nudY.Value;
            int nHeight = (int)nudHeight.Value;

            int y = 0;
            int x = 0;
            if (nWidth != dataGridView1.Columns.Count && nHeight != dataGridView1.Rows.Count)
            {
                this.dataGridView1.Columns.Clear();
                this.dataGridView1.Rows.Clear();

                for (x = 0; x < nWidth - nMinX; x++)
                {
                    dataGridView1.Columns.Add(new DataGridViewColumn(new DataGridViewTextBoxCell()));
                    dataGridView1.Columns[x].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
                }
                for (y = 0; y < nHeight - nMinY; y++)
                {
                    dataGridView1.Rows.Add();
                }

            }

            ColorCalculation calculation = Definitions.ColorCalculationDictionary[(EXPERIMENTAL1)this.cobMode.SelectedItem];
            Color color = Color.Transparent;
            DataGridViewCell cell = null;
            float fValue = 0.0f;

            for (y = nMinY; y < nHeight; y++)
            {
                for (x = nMinX; x < nWidth; x++)
                {
                    color = bitmap.GetPixel(x, y);
                    fValue = calculation(color);
                    cell = dataGridView1.Rows[y - nMinY].Cells[x - nMinX];
                    if (m_bPadding)
                    {
                        cell.Value = string.Format("{0:00.0000}", fValue);
                    }
                    else
                    {
                        cell.Value = fValue;
                    }
                    cell.Style.ForeColor = ColorFunctions.Invert(color);
                    cell.Style.BackColor = color;
                }
            }
            bitmap.Dispose();
            this.dataGridView1.ResumeLayout();
        }

        /// <summary>
        /// Actualize display
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbActualize_Click(object sender, EventArgs e)
        {
            DisplayValues();
        }

        /// <summary>
        /// when the check state changes set the padding
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkbPadding_CheckedChanged(object sender, EventArgs e)
        {
            this.m_bPadding = chkbPadding.Checked;
        }
    }
}
